<?php

class Page_Fe_Products_Step3 extends Page {
	/**
	 * @var Db_OrderAddress
	 */
	protected $tableObject;

    function __construct() {
        $this->idTableObject = get('id', 'int');
        $this->actionsTableObject = array(
            'defaultAction'
        );
        $this->classTableObject = 'Db_OrderAddress';
        //self::$TPL->appendCssFile('wedding/step3');
        //self::$TPL->appendJsFile('wedding/step3');
        //self::$TPL->setBodyTemplate('wedding/step3');
        parent::__construct();
    }

    public function defaultAction() {
		self::$S->Remove('formData');
		$orderId = self::$S->Get('orderId');
        /**
         * if have value = '1' we make one insert in db with type general
         * else if value = '2' we make two insert in db with type invoice and delivery
         */

		Db_OrderAddress::deleteByField('ad_orders_id', $orderId, 0);
        $sameType = post('same_address','int');
        $invoiceCompanyName = post('invoice_company_name', 'string');
        $invoiceCity = post('invoice_city', 'string');
        $invoiceEmailAddress = post('invoice_address_email', 'string');
        $invoicePostCode = post('invoice_postcode', 'string');
        $invoiceNumber = post('invoice_number', 'string');
        $invoiceFirstName = post('invoice_first_name', 'string');
        $invoiceName = post('invoice_name', 'string');
		$invoicePackStation = (post('invoice_packstation', 'int') != null) ? post('invoice_pack_station', 'string') : '';
        $invoiceStreet = post('invoice_street', 'string');
        $invoiceTelephone = post('invoice_telephone', 'string');
        $invoiceCountryId = post('invoice_country', 'int');
        $invoiceSolutionId = post('invoice_salutation_id', 'int');

        if($this->idTableObject) {
            $this->tableObject->id = $this->idTableObject;
        }
        $this->tableObject->ad_company_name = $invoiceCompanyName;
        $this->tableObject->ad_city = $invoiceCity;
        $this->tableObject->ad_email = $invoiceEmailAddress;
        $this->tableObject->ad_post_code = $invoicePostCode;
        $this->tableObject->ad_number = $invoiceNumber;
        $this->tableObject->ad_first_name = $invoiceFirstName;
        $this->tableObject->ad_name = $invoiceName;
        $this->tableObject->ad_pack_station = $invoicePackStation;
        $this->tableObject->ad_street = $invoiceStreet;
        $this->tableObject->ad_telephone = $invoiceTelephone;
        $this->tableObject->ad_country_id = $invoiceCountryId;
        $this->tableObject->ad_solution_id = $invoiceSolutionId;
        $this->tableObject->ad_orders_id = $orderId;

        if ($sameType == 1 &&!empty($invoiceEmailAddress) && isset($invoiceSolutionId) && is_int($invoiceSolutionId) && !empty($invoiceTelephone) && !empty($invoiceStreet) &&
            isset($invoiceCountryId) && is_int($invoiceCountryId) && !empty($invoiceNumber) && (!empty($invoiceCompanyName) || !empty($invoiceFirstName)) && !empty($invoicePostCode) &&
            !empty($invoiceName) && !empty($invoiceCity)) {
            $this->tableObject->ad_type = 'general';

            if (!$this->tableObject->save(true)) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        } elseif ($sameType == 2) {
            if (true) {
                $this->tableObject->ad_type = 'invoice';
                if (!$this->tableObject->save()) {
                    $this->error = true;
                    $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
                }
            } else {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }

            $this->tableObject->id = null;
            $this->tableObject->ad_type = 'delivery';
            $deliveryCompanyName = post('delivery_company_name', 'string');
            $deliveryCity = post('delivery_city', 'string');
            $deliveryPostCode = post('delivery_postcode', 'string');
            $deliveryNumber = post('delivery_number', 'string');
            $deliveryFirstName = post('delivery_first_name', 'string');
            $deliveryName = post('delivery_name', 'string');
			$deliveryPackStation =  (post('delivery_packstation', 'int') != null) ? post('delivery_pack_station', 'str') : null;
			$deliveryStreet = post('delivery_street', 'string');
            $deliveryTelephone = post('delivery_telephone', 'string');
            $deliveryCountryId = post('delivery_country', 'int');
            $deliverySolutionId = post('delivery_salutation_id', 'int');

            if (isset($deliverySolutionId) && is_int($deliverySolutionId) && !empty($deliveryTelephone) && !empty($deliveryStreet) && isset($deliveryCountryId) &&
                is_int($deliveryCountryId) && !empty($deliveryNumber) && (!empty($deliveryCompanyName) || !empty($deliveryFirstName)) && !empty($deliveryPostCode) &&
                !empty($deliveryName) && !empty($deliveryCity)) {
                $this->tableObject->ad_company_name = $deliveryCompanyName;
                $this->tableObject->ad_city = $deliveryCity;
                $this->tableObject->ad_telephone = $deliveryTelephone;
                $this->tableObject->ad_post_code = $deliveryPostCode;
                $this->tableObject->ad_number = $deliveryNumber;
                $this->tableObject->ad_first_name = $deliveryFirstName;
                $this->tableObject->ad_name = $deliveryName;
                $this->tableObject->ad_pack_station = $deliveryPackStation;
                $this->tableObject->ad_street = $deliveryStreet;
                $this->tableObject->ad_country_id = $deliveryCountryId;
                $this->tableObject->ad_solution_id = $deliverySolutionId;
                $this->tableObject->ad_orders_id = $orderId;

                if (!$this->tableObject->save()) {
                    $this->error = true;
                    $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
                }
            }
        } else {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        }

		self::$TPL->assign('phoneNumber',self::$SETTINGS['phone_number']);
		self::$TPL->setBodyTemplate('last_step');
        return array(
            'type' => 'display',
        );
    }
}